package byteMath

import (
	"fmt"
	"strconv"
)

func Byte2Str(b byte, targetBase int) (string, error) {
	parseInt, err := strconv.ParseInt(fmt.Sprintf("%d", b), 10, 64)
	if err != nil {
		return "", err
	}
	rs := strconv.FormatInt(parseInt, targetBase)
	if targetBase == 2 {
		for {
			if len(rs) < 8 {
				rs = "0" + rs
			} else {
				break
			}
		}
	}
	return rs, nil
}

func Str2Byte(str string, originBase int) (byte, error) {
	parseInt, err := strconv.ParseInt(str, originBase, 64)
	if err != nil {
		return 0, err
	}
	return uint8(parseInt), nil
}

func Int2Str(i int, targetBase int) (string, error) {
	parseInt, err := strconv.ParseInt(fmt.Sprintf("%d", i), 10, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(parseInt, targetBase), nil
}

func Str2Int(str string, originBase int) (int, error) {
	parseInt, err := strconv.ParseInt(str, originBase, 64)
	if err != nil {
		return 0, err
	}
	return int(parseInt), nil
}
